<?php

namespace frontend\models;

use Yii;
use paulzi\materializedPath\MaterializedPathBehavior;

/**
 * This is the model class for table "page".
 *
 * @property int $id
 * @property string $title
 * @property string $alias
 * @property string $description
 * @property string $content
 * @property string $path
 * @property int $depth
 * @property int $sort
 */
class Page extends \yii\db\ActiveRecord
{
    const DRAFT = 1;
    const PUBLISHED = 0;
    const IS_MODULE = 1;
    const IS_STATIC_PAGE = 0;
    
    const HIDDEN = 1;
    const VISIBLE = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'page';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['title', 'alias', 'description', 'depth', 'sort'], 'required'],
            [['content'], 'string'],
            [['module_url_rule'], 'safe'],
            [['depth', 'sort', 'is_draft', 'is_hidden'], 'integer'],
            [['title', 'alias', 'description', 'path', 'view'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'alias' => 'Alias',
            'description' => 'Description',
            'content' => 'Content',
            'path' => 'Path',
            'depth' => 'Depth',
            'sort' => 'Sort',
        ];
    }

    public function behaviors() {
        return [
            [
                'class' => MaterializedPathBehavior::className(),
                // 'treeAttribute' => 'tree',
            ],
        ];
    }

    public function transactions()
    {
        return [
            self::SCENARIO_DEFAULT => self::OP_ALL,
        ];
    }

    public static function find()
    {
        return new PageQuery(get_called_class());
    }

    /**
     * Gets query for [[Child]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getRedirect()
    {
        return $this->hasMany(PageRedirect::class, ['parent_id' => 'id']);
    }
}
